import mysql.JDBC;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.nio.ByteBuffer;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.util.Timer;

public class Lib_JPanel {
    private Book book=new Book();
    private static User user;
    GetIcon gi = new GetIcon();
    //JTable BookreportTable= new JTable();
    ArrayList<Book> BookList = new ArrayList<Book>();
    static ArrayList<User> UserList = new ArrayList<User>();
    static ArrayList<Borrow> OverBorrowList = new ArrayList<Borrow>();
    static ArrayList<Type> TypeList = new ArrayList<Type>();

    static ArrayList<Borrow> AllborrowList = new ArrayList<Borrow>();
    static ArrayList<Operation> OperationList = new ArrayList<Operation>();


    public Lib_JPanel(User user) {
        this.user = user;
        // 在这里可以使用 user 对象进行操作
        // 例如：this.user.getUsername() 等等
    }
    public Lib_JPanel() {
    }

    private static JLabel getJLabel(String text, Color fore_color, Color back_color, String font_name, int size, int width, int height){
        JLabel label = new JLabel(text);
        Font font = new Font(font_name, Font.BOLD, size);
        label.setBackground(back_color);
        label.setFont(font);
        label.setForeground(fore_color);
        label.setSize(width, height);
        return label;
    }
    public JPanel createHomePanel() {
        SelectOverBorrow();
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JLabel titleLabel = new JLabel("主页");
        titleLabel.setFont(new Font("仿宋", Font.PLAIN, 12));
        panel.add(titleLabel, BorderLayout.NORTH);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setResizeWeight(0.5);

        JPanel topPanel = new JPanel(new GridBagLayout());
        topPanel.setBackground(Color.WHITE);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel label1 = getJLabel("<html>图书馆书本数量<br>" + getbook_number() + "</html>", Color.WHITE, Color.CYAN, "黑体", 60, 40, 24);
        JLabel label2 = getJLabel("<html>借出书本数量<br>" + getborrow_number()+ "</html>", Color.WHITE, Color.GREEN, "黑体", 60, 40, 24);
        JLabel label3 = getJLabel("<html>超时归还数量<br>"+OverBorrowList.size()+ "</html>", Color.WHITE, Color.PINK, "黑体", 60, 40, 24);
        JLabel label4 = getJLabel("<html>系统时间<br>"+getCurrentTime()+ "</html>", Color.WHITE, Color.ORANGE, "黑体", 40, 40, 24);

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                label4.setText("<html>系统时间<br>"+getCurrentTime()+ "</html>");
            }
        };
        timer.scheduleAtFixedRate(task, 0, 1000);

        label1.setOpaque(true);
        label2.setOpaque(true);
        label3.setOpaque(true);
        label4.setOpaque(true);

        label1.setHorizontalAlignment(SwingConstants.CENTER);
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        label4.setHorizontalAlignment(SwingConstants.CENTER);

        // 设置标签字体大小
        Font labelFont = label1.getFont();
        Font newFont = labelFont.deriveFont(Font.BOLD, 25); // 增大字体大小
        label1.setFont(newFont);
        label2.setFont(newFont);
        label3.setFont(newFont);
        label4.setFont(newFont);

        // 添加标签图标
        Icon icon1 = gi.getIcon("图书馆书本数量.png", 70, 70);
        Icon icon2 = gi.getIcon("借出书本数量.png", 70, 70);
        Icon icon3 = gi.getIcon("超时归还数量.png", 70, 70);
        Icon icon4 = gi.getIcon("系统时间.png", 70, 70);

        label1.setIcon(icon1);
        label2.setIcon(icon2);
        label3.setIcon(icon3);
        label4.setIcon(icon4);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 0.5;
        topPanel.add(label1, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        topPanel.add(label2, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        topPanel.add(label3, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        topPanel.add(label4, gbc);

        splitPane.setTopComponent(topPanel);


        JPanel bottomPanel = new JPanel(new BorderLayout());
        String[] columnNames = {"日期时间", "名字", "信息"};
        Object[][] data = new Object[OperationList.size()][columnNames.length];

        JButton updateButton = new JButton("更新");
        updateButton.setBorder(BorderFactory.createEmptyBorder()); // 设置无边框
        updateButton.setBackground(Color.CYAN); // 设置背景颜色为蓝色
        DefaultTableModel model = new DefaultTableModel(data, columnNames);

        bottomPanel.add(updateButton, BorderLayout.PAGE_START); // 将更新按钮添加到标题标签的旁边

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OperationList.clear();;
                Selectoperation();

            }
        });

        JTable reportTable = new JTable(data, columnNames);
        JLabel titleLabel2 = new JLabel("操作日志");
        titleLabel.setFont(new Font("仿宋", Font.PLAIN, 12));
        bottomPanel.add(titleLabel2, BorderLayout.NORTH);
        OperationList.clear();
        Selectoperation();

        for (Operation operation : OperationList) {
            if (operation.getName() != null) {
                Object[] rowData = {operation.getTime(), operation.getName(), operation.getInfo()};
                model.addRow(rowData); // 将符合条件的数据添加到新的表格模型
            }
        }
        reportTable.setModel(model);



        Font tableFont = reportTable.getFont();
        Font largerFont = tableFont.deriveFont(tableFont.getSize() * 1.3f);
        reportTable.setFont(largerFont);
        reportTable.setRowHeight((int) (reportTable.getRowHeight() * 1.3));
        reportTable.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        JScrollPane scrollPane = new JScrollPane(reportTable);
        bottomPanel.add(scrollPane, BorderLayout.CENTER);
        splitPane.setBottomComponent(bottomPanel);

        panel.add(splitPane, BorderLayout.CENTER);

        return panel;
    }
    public JPanel createBookManagementPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 创建用户管理子夹的页面组件
        JLabel titleLabel = new JLabel("图书管理页面");
        titleLabel.setFont(titleLabel.getFont().deriveFont(24f));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(titleLabel, BorderLayout.NORTH);

        // 创建顶部面板
        JPanel topPanel = new JPanel(new BorderLayout());

        // 创建左侧面板容纳组合框
        JPanel leftPanel = new JPanel(new BorderLayout());



        // 创建用户名选择组合框
        JComboBox<String> usernameComboBox = new JComboBox<>();
        // 添加用户名选项
        usernameComboBox.addItem("全部");
        usernameComboBox.addItem("书名");
        usernameComboBox.addItem("ISBN码");
        usernameComboBox.addItem("作者");

        // 设置组合框大小为原来的2倍

        leftPanel.add(usernameComboBox, BorderLayout.EAST);
        topPanel.add(leftPanel, BorderLayout.WEST);
        // 创建右侧面板容纳搜索框和按钮
        JPanel rightPanel = new JPanel(new BorderLayout());
        // 创建搜索框
        JTextField searchField = new JTextField();
        searchField.setToolTipText("请输入搜索内容");
        // 将搜索框变窄为原来的1倍
        Dimension searchFieldSize = searchField.getPreferredSize();
        searchFieldSize.width /= 2;
        searchField.setPreferredSize(searchFieldSize);
        rightPanel.add(searchField, BorderLayout.CENTER);

        // 创建搜索按钮
        JButton searchButton = new JButton("搜索");
        searchButton.setOpaque(true); // 设置按钮为不透明
        searchButton.setBackground(new Color(135, 206, 250));
        searchButton.setForeground(Color.white);
        searchButton.setBorderPainted(false); // 不绘制按钮边框
        rightPanel.add(searchButton, BorderLayout.EAST);

        // 添加用户按钮
        Font font1 = new Font("仿宋", Font.BOLD, 16);
        JButton addUserButton = new JButton("添加图书");
        addUserButton.setOpaque(true);
        addUserButton.setBackground(new Color(153, 217, 154));
        addUserButton.setFont(font1);
        addUserButton.setForeground(Color.white);
        addUserButton.setBorderPainted(false);
        //rightPanel.add(addUserButton, BorderLayout.SOUTH);
        addUserButton.addActionListener(e -> {
            System.out.println("添加图书");
            AddBookDialog();
        });

        topPanel.add(rightPanel, BorderLayout.CENTER);

        panel.add(topPanel, BorderLayout.NORTH);
        JPanel bottomPanel = new JPanel(new BorderLayout());


        // 创建表格模型
        String[] columnNames = {"ID", "组别", "书名", "作者", "出版社", "价格（人名币）", "数量（本）", "ISBN号码"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0); // 初始化空模型
        JTable bookreportTable = new JTable(model);

        // 创建滚动面板并添加表格
        JScrollPane scrollPane = new JScrollPane(bookreportTable);
        bottomPanel.add(scrollPane, BorderLayout.CENTER);
        Object[][] data = new Object[BookList.size()][columnNames.length];
        // 创建报表表格
        for (int i = 0; i < BookList.size(); i++) {
            Book book = BookList.get(i);
            data[i][0] = book.getId();
            data[i][1] = book.getGroups();
            data[i][2] = book.getName();
            data[i][3] = book.getAuthor();
            data[i][4] = book.getPress();
            data[i][5] = book.getPrice();
            data[i][6] = book.getQuantity();
            data[i][7] = book.getIsbn();
        }
        model = new DefaultTableModel(data, columnNames);
        bookreportTable.setModel(model);
        searchButton.addActionListener(e -> {
            String searchText = searchField.getText();
            String selectOption = (String) usernameComboBox.getSelectedItem();
            DefaultTableModel newModel = new DefaultTableModel(columnNames, 0); // 创建新的表格模型

            for (Book book : BookList) {
                if (selectOption.equals("全部") ||
                        (selectOption.equals("书名") && book.getName().equals(searchText)) ||
                        (selectOption.equals("ISBN码") && book.getIsbn().equals(searchText)) ||
                        (selectOption.equals("作者") && book.getAuthor().equals(searchText))) {
                    Object[] rowData = {book.getId(), book.getGroups(), book.getName(), book.getAuthor(), book.getPress(), book.getPrice(), book.getQuantity(), book.getIsbn()};
                    newModel.addRow(rowData); // 将符合条件的数据添加到新的表格模型
                }
            }
            bookreportTable.setModel(newModel); // 更新表格模型
        });



        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(addUserButton, BorderLayout.SOUTH);

        // 创建编辑用户按钮
        Font font = new Font("仿宋", Font.BOLD, 16);
        JButton editUserButton = new JButton("编辑图书");
        editUserButton.setOpaque(true);
        editUserButton.setBackground(new Color(135, 206, 250));
        editUserButton.setForeground(Color.white);
        editUserButton.setBorderPainted(false);
        editUserButton.setFont(font);
        editUserButton.setPreferredSize(new Dimension(100, 30));
        buttonPanel.add(editUserButton);
        editUserButton.addActionListener(e -> {
            System.out.println("编辑图书");
            UpdateBookDialog();
        });


        // 创建删除用户按钮
        JButton deleteUserButton = new JButton("删除图书");
        deleteUserButton.setOpaque(true);
        deleteUserButton.setBackground(Color.CYAN);
        deleteUserButton.setForeground(Color.white);
        deleteUserButton.setBorderPainted(false);
        deleteUserButton.setFont(font);
        deleteUserButton.setPreferredSize(new Dimension(100, 30));
        buttonPanel.add(deleteUserButton);
        deleteUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createCenteredDialog();
            }
        });

        bottomPanel.add(buttonPanel, BorderLayout.SOUTH);

        panel.add(bottomPanel, BorderLayout.CENTER);
        return panel;

    }
    public JPanel createBorrowBookPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 创建借阅图书功能组件面板
        JPanel borrowPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;

        // 创建标题标签
        JLabel titleJLabel = getJLabel("借阅图书", Color.BLACK, Color.WHITE, "黑体", 30, 40, 40);
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new Insets(0, 0, 10, 0); // 添加间距
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        borrowPanel.add(titleJLabel, gridBagConstraints);

        // 创建搜索框
        JTextField searchField = new JTextField();
        searchField.setPreferredSize(new Dimension(200, 30));
        searchField.addFocusListener(getFocusListener(searchField,"请输入要借阅的ISBN号码"));
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(searchField, gridBagConstraints);

        // 创建搜索按钮
        JButton searchButton = new JButton("搜索");

        searchButton.setOpaque(true);
        searchButton.setBackground(new Color(135, 206, 250));
        searchButton.setForeground(Color.white);
        searchButton.setBorderPainted(false);
        searchButton.setPreferredSize(new Dimension(70, 30));
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new Insets(0, 5, 10, 0); // 添加间距
        borrowPanel.add(searchButton, gridBagConstraints);
        // 创建图书信息标签
        JLabel bookInfoLabel = getJLabel("图书信息",Color.BLACK,Color.WHITE,"黑体",20,25,25);
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new Insets(10, 0, 10, 0); // 添加间距
        borrowPanel.add(bookInfoLabel, gridBagConstraints);

        // 创建图书名字标签
        JLabel bookNameLabel = new JLabel("图书名字:");
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(bookNameLabel, gridBagConstraints);

        // 创建图书名字输入框
        JTextField borrowuser_name = new JTextField();
        borrowuser_name.addFocusListener(getFocusListener(borrowuser_name,"借阅人名字"));
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new Insets(0, 5, 10, 0); // 添加间距
        borrowPanel.add(borrowuser_name, gridBagConstraints);

        // 创建图书作者标签
        JLabel authorLabel = new JLabel("作者:");
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(authorLabel, gridBagConstraints);

        // 创建图书作者输入框
        JTextField borrow_iphone = new JTextField();
        borrow_iphone.addFocusListener(getFocusListener(borrow_iphone,"借阅人手机号"));
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new Insets(0, 5, 10, 0); // 添加间距
        borrowPanel.add(borrow_iphone, gridBagConstraints);

        // 创建出版社名称标签
        JLabel publisherLabel = new JLabel("出版社名称:");
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(publisherLabel, gridBagConstraints);


        // 创建ISBN码标签
        JLabel isbnLabel = new JLabel("ISBN码:");
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(isbnLabel, gridBagConstraints);

        // 创建ISBN码输入框
        JTextField borrow_id_card = new JTextField();
        borrow_id_card.addFocusListener(getFocusListener(borrow_id_card,"借阅人卡号"));
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new Insets(0, 5, 10, 0); // 添加间距

        borrowPanel.add(borrow_id_card, gridBagConstraints);

        // 创建库存标签
        JLabel stockLabel = new JLabel("库存（本）:");
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.insets = new Insets(0, 0, 10, 5); // 添加间距
        borrowPanel.add(stockLabel, gridBagConstraints);

        // 创建库存输入框
        JButton Borrowing = new JButton("借阅图书");
        Borrowing.setOpaque(true);
        Borrowing.setForeground(Color.white);
        Borrowing.setBackground(new Color(135, 206, 250));
        Borrowing.setBorderPainted(false);
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.insets = new Insets(0, 5, 10, 0); // 添加间距
        borrowPanel.add(Borrowing, gridBagConstraints);

        // 创建用户信息标签
        JLabel userInfoLabel = getJLabel("用户信息",Color.BLACK,Color.WHITE,"黑体",20,25,25);

        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(10, 0, 10, 0); // 添加间距
        borrowPanel.add(userInfoLabel, gridBagConstraints);
        panel.add(borrowPanel, BorderLayout.CENTER);

        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String searchContent = searchField.getText();
                book = SelectISBNbook(searchContent);
                if (book != null) {
                    bookNameLabel.setText("图书名字：" + book.getName());
                    publisherLabel.setText("出版社名称：" + book.getPress());
                    authorLabel.setText("作者：" + book.getAuthor());
                    isbnLabel.setText("ISBN码：" + book.getIsbn());
                    stockLabel.setText("库存（本）：" + book.getQuantity());
                } else {
                    showJDialog("未找到匹配的图书");
                }
            }
        });

        Borrowing.addActionListener(e -> {
            if (book != null && !borrowuser_name.getText().isEmpty()) {
                if (updateBookquantity(book.getIsbn())) {
                    AddBorrowBook(user.getId(), book.getName(), book.getIsbn(), borrowuser_name.getText(), borrow_id_card.getText(), borrow_iphone.getText());
                    AddOperation(OperationList.size()+1,user.getName(), book.getName(), "借书");
                    System.out.println("借阅成功！！");
                    showJDialog("借阅成功！！");
                } else {
                    showJDialog("图书库存不足");
                }
            } else {
                showJDialog("请先搜索图书并填写借阅信息");
            }
        });
        return panel;
    }
    public JPanel createReturnBookPanel() {
        JPanel panel = new JPanel();
        JPanel topPanel = new JPanel(new BorderLayout());
        JPanel leftPanel = new JPanel(new BorderLayout());
        JPanel rightPanel = new JPanel(new BorderLayout());

        panel.setLayout(new BorderLayout());
        String[] columnNames = {"ID", "书名", "ISBN码", "借阅人", "借阅卡号", "借阅时间", "应归还时间"};

        Object[][] data = new Object[AllborrowList.size()][columnNames.length];

        // 创建超时查询功能组件面板
        JPanel searchPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH; // 填充水平和垂直空间
        gridBagConstraints.weightx = 1.0; // 设置水平权重
        gridBagConstraints.weighty = 1.0; // 设置垂直权重



        // 创建搜索框
        JTextField searchField = new JTextField();
        searchField.setPreferredSize(new Dimension(150, 20));
        searchField.setToolTipText("请输入搜索内容");
        // 将搜索框变窄为原来的1倍
        Dimension searchFieldSize = searchField.getPreferredSize();
        searchFieldSize.width /= 2;
        searchField.setPreferredSize(searchFieldSize);
        rightPanel.add(searchField, BorderLayout.CENTER);

        // 添加用户名选项下拉框
        JComboBox<String> usernameComboBox = new JComboBox<>();
        usernameComboBox.setPreferredSize(new Dimension(100, 20));
        usernameComboBox.addItem("全部");
        usernameComboBox.addItem("书名");
        usernameComboBox.addItem("id");
        usernameComboBox.addItem("借阅人");

        leftPanel.add(usernameComboBox, BorderLayout.EAST);
        topPanel.add(leftPanel, BorderLayout.WEST);

        // 创建搜索按钮
        JButton searchButton = new JButton("搜索");
        searchButton.setOpaque(true); // 设置按钮为不透明
        searchButton.setBackground(new Color(135, 206, 250));
        searchButton.setForeground(Color.white);
        searchButton.setBorderPainted(false); // 不绘制按钮边框
        rightPanel.add(searchButton, BorderLayout.EAST);
        topPanel.add(rightPanel, BorderLayout.CENTER);
        panel.add(topPanel,BorderLayout.NORTH);



        JTable overdueTable = new JTable(data, columnNames);
        searchButton.addActionListener(e -> {
            String searchText = searchField.getText();
            String selectOption = (String) usernameComboBox.getSelectedItem();
            DefaultTableModel newModel = new DefaultTableModel(columnNames, 0);
            for (Borrow borrow : AllborrowList) {
                if (selectOption.equals("全部") ||
                        (selectOption.equals("书名") && borrow.getBook_name().equals(searchText)) ||
                        (selectOption.equals("ISBN码") && borrow.getIsbn().equals(searchText)) ||
                        (selectOption.equals("借阅人") && borrow.getUsername().equals(searchText))) {
                    Object[] rowData = {borrow.getId(), borrow.getBook_name(), borrow.getIsbn(), borrow.getUsername(), borrow.getId_card(), borrow.getBorrow_time(), borrow.getReturn_time()};
                    newModel.addRow(rowData);
                }
            }
            overdueTable.setModel(newModel);
        });

        // 创建超级列表框

        for (int i = 0; i < AllborrowList.size(); i++) {
            Borrow borrow = AllborrowList.get(i);
            data[i][0] = borrow.getId();
            data[i][1] = borrow.getBook_name();
            data[i][2] = borrow.getIsbn();
            data[i][3] = borrow.getUsername();
            data[i][4] = borrow.getId_card();
            data[i][5] = borrow.getBorrow_time();

            // 设置应归还时间为借阅时间加10分钟
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(borrow.getBorrow_time());
            calendar.add(Calendar.MINUTE, 10);
            data[i][6] = calendar.getTime();
        }

        overdueTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane scrollPane = new JScrollPane(overdueTable);
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new Insets(10, 0, 0, 0); // 添加间距
        searchPanel.add(scrollPane, gridBagConstraints);
        panel.add(searchPanel, BorderLayout.CENTER);

        // 创建归还图书按钮
        JButton returnButton = new JButton("归还图书");
        returnButton.setPreferredSize(new Dimension(30, 20));
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new Insets(10, 0, 0, 0); // 添加间距
        returnButton.addActionListener(e -> {
            DeleteBorrowDialog();
        });
        searchPanel.add(returnButton, gridBagConstraints);
        overdueTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        return panel;
    }

    public JPanel createOverdueQueryPanel() {
        JPanel panel = new JPanel();
        JPanel topPanel = new JPanel(new BorderLayout());
        JPanel leftPanel = new JPanel(new BorderLayout());
        JPanel rightPanel = new JPanel(new BorderLayout());
        panel.setLayout(new BorderLayout());
        String[] columnNames = {"ID", "书名", "ISBN码", "借阅人", "借阅卡号", "借阅时间", "应归还时间"};
        Object[][] data = new Object[OverBorrowList.size()][columnNames.length];
        JTable overdueTable = new JTable(data, columnNames);

        JComboBox<String> usernameComboBox = new JComboBox<>();
        usernameComboBox.setPreferredSize(new Dimension(100, 20));
        usernameComboBox.addItem("全部");
        usernameComboBox.addItem("书名");
        usernameComboBox.addItem("id");
        usernameComboBox.addItem("借阅人");

        JTextField searchField = new JTextField();
        searchField.setToolTipText("请输入搜索内容");
        // 将搜索框变窄为原来的1倍
        Dimension searchFieldSize = searchField.getPreferredSize();
        searchFieldSize.width /= 2;
        searchField.setPreferredSize(searchFieldSize);
        rightPanel.add(searchField, BorderLayout.CENTER);


        JButton searchButton = new JButton("搜索");
        searchButton.setOpaque(true); // 设置按钮为不透明
        searchButton.setBackground(new Color(135, 206, 250));
        searchButton.setForeground(Color.white);
        searchButton.setBorderPainted(false); // 不绘制按钮边框
        rightPanel.add(searchButton, BorderLayout.EAST);
        leftPanel.add(usernameComboBox, BorderLayout.EAST);

        searchButton.addActionListener(e -> {
            String searchText = searchField.getText();
            String selectOption = (String) usernameComboBox.getSelectedItem();
            DefaultTableModel newModel = new DefaultTableModel(columnNames, 0);
            for (Borrow borrow : AllborrowList) {
                if (selectOption.equals("全部") ||
                        (selectOption.equals("书名") && borrow.getBook_name().equals(searchText)) ||
                        (selectOption.equals("ISBN码") && borrow.getIsbn().equals(searchText)) ||
                        (selectOption.equals("借阅人") && borrow.getUsername().equals(searchText))) {
                    Object[] rowData = {borrow.getId(), borrow.getBook_name(), borrow.getIsbn(), borrow.getUsername(), borrow.getId_card(), borrow.getBorrow_time(), borrow.getReturn_time()};
                    newModel.addRow(rowData);
                }
            }
            overdueTable.setModel(newModel);
        });

        topPanel.add(leftPanel, BorderLayout.WEST);
        topPanel.add(rightPanel, BorderLayout.CENTER);
        JPanel bottomPanel = new JPanel(new BorderLayout());
        // 创建超级列表框



        for (int i = 0; i < OverBorrowList.size(); i++) {
            Borrow borrow = OverBorrowList.get(i);
            data[i][0] = borrow.getId();
            data[i][1] = borrow.getBook_name();
            data[i][2] = borrow.getIsbn();
            data[i][3] = borrow.getUsername();
            data[i][4] = borrow.getId_card();
            data[i][5] = borrow.getBorrow_time();
            data[i][6] = borrow.getReturn_time();
            System.out.println("已将"+i+"条超时归还的图书加入OverBorrowList");

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(borrow.getBorrow_time());
            calendar.add(Calendar.MINUTE, 10);
            data[i][6] = calendar.getTime();
        }

        JScrollPane scrollPane = new JScrollPane(overdueTable);
        bottomPanel.add(scrollPane, BorderLayout.CENTER);
        overdueTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        overdueTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        panel.add(topPanel,BorderLayout.NORTH);
        panel.add(bottomPanel, BorderLayout.CENTER);

        return panel;
    }
    public JPanel createUserManagementPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        String[] columnNames = {"ID", "组别", "姓名", "用户名", "密码", "性别", "借书卡号", "手机号","身份","可结束状态","状态"};
        Object[][] data = new Object[UserList.size()][columnNames.length];
        JTable reportTable = new JTable(data, columnNames);

        // 创建用户管理子夹的页面组件
        JLabel titleLabel = new JLabel("用户管理页面");
        titleLabel.setFont(titleLabel.getFont().deriveFont(24f));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(titleLabel, BorderLayout.NORTH);

        // 创建顶部面板
        JPanel topPanel = new JPanel(new BorderLayout());

        // 创建左侧面板容纳组合框
        JPanel leftPanel = new JPanel(new BorderLayout());



        // 创建用户名选择组合框
        JComboBox<String> usernameComboBox = new JComboBox<>();
        // 添加用户名选项
        usernameComboBox.addItem("用户名");
        usernameComboBox.addItem("ID");
        usernameComboBox.addItem("全部");
        // 设置组合框大小为原来的2倍
        leftPanel.add(usernameComboBox, BorderLayout.EAST);
        topPanel.add(leftPanel, BorderLayout.WEST);

        // 创建右侧面板容纳搜索框和按钮
        JPanel rightPanel = new JPanel(new BorderLayout());

        // 创建搜索框
        JTextField searchField = new JTextField();
        searchField.setToolTipText("请输入搜索内容");
        // 将搜索框变窄为原来的1倍
        Dimension searchFieldSize = searchField.getPreferredSize();
        searchFieldSize.width /= 2;
        searchField.setPreferredSize(searchFieldSize);
        rightPanel.add(searchField, BorderLayout.CENTER);

        // 创建搜索按钮
        JButton searchButton = new JButton("搜索");
        searchButton.setOpaque(true); // 设置按钮为不透明
        searchButton.setBackground(Color.cyan);
        searchButton.setForeground(Color.white);
        searchButton.setBorderPainted(false); // 不绘制按钮边框
        rightPanel.add(searchButton, BorderLayout.EAST);
        searchButton.addActionListener(e -> {
            String searchText = searchField.getText();
            String selectOption = (String) usernameComboBox.getSelectedItem();
            DefaultTableModel newModel = new DefaultTableModel(columnNames, 0); // 创建新的表格模型

            for (User user : UserList) {
                if (selectOption.equals("全部") ||
                        (selectOption.equals("姓名") && user.getName().equals(searchText)) ||
                        (selectOption.equals("用户名") && user.getUsername().equals(searchText)) ||
                        (selectOption.equals("组别") && user.getGroups().equals(searchText))){
                    Object[] rowData = {user.getId(),user.getGroups(),user.getName(),user.getUsername(),user.getPassword(),user.getGender(),user.getId_card(),user.getPhone(),user.getIdentity(),user.getBook_count(),user.getState()};
                    newModel.addRow(rowData); // 将符合条件的数据添加到新的表格模型
                }
            }
            reportTable.setModel(newModel); // 更新表格模型
        });

        // 添加用户按钮
        JButton addUserButton = new JButton("添加用户");
        Font font = new Font("仿宋", Font.BOLD, 16);
        addUserButton.setOpaque(true);
        addUserButton.setFont(font);
        addUserButton.setBackground(new Color(153, 217, 154));
        addUserButton.setForeground(Color.white);
        addUserButton.setBorderPainted(false);
        //rightPanel.add(addUserButton, BorderLayout.SOUTH);
        addUserButton.addActionListener(e -> {
            System.out.println("添加用户");
            createAddUserDialog();
        });

        topPanel.add(rightPanel, BorderLayout.CENTER);

        panel.add(topPanel, BorderLayout.NORTH);

        // 创建底部面板
        JPanel bottomPanel = new JPanel(new BorderLayout());

        // 创建报表表格

        for (int i = 0; i < UserList.size(); i++) {
            User user = UserList.get(i);
            data[i][0] = user.getId();
            data[i][1] = user.getName();
            data[i][2] = user.getGroups();
            data[i][3] = user.getUsername();
            data[i][4] = user.getPassword();
            data[i][5] = user.getGender();
            data[i][6] = user.getId_card();
            data[i][7] = user.getPhone();
            data[i][8] = user.getIdentity();
            data[i][9] = user.getBook_count();
            data[i][10] = user.getState();
        }

        reportTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane scrollPane = new JScrollPane(reportTable);
        bottomPanel.add(scrollPane, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        // 创建编辑用户按钮

        JButton editUserButton = new JButton("编辑用户");
        editUserButton.setOpaque(true);
        editUserButton.setBackground(new Color(135, 206, 250));
        editUserButton.setForeground(Color.white);
        editUserButton.setBorderPainted(false);
        editUserButton.setFont(font);
        editUserButton.setPreferredSize(new Dimension(100, 30));

        editUserButton.addActionListener(e -> {
            System.out.println("编辑用户");
            UpdateUserDialog();
        });

        // 创建删除用户按钮
        JButton deleteUserButton = new JButton("删除用户");
        deleteUserButton.setOpaque(true);
        deleteUserButton.setBackground(new Color(236, 80, 80));
        deleteUserButton.setForeground(Color.white);
        deleteUserButton.setBorderPainted(false);
        deleteUserButton.setFont(font);
        deleteUserButton.setPreferredSize(new Dimension(100, 30));
        deleteUserButton.addActionListener(e -> {
            System.out.println("删除用户");
            DeleteUserDialog();
        });
        buttonPanel.add(addUserButton, BorderLayout.SOUTH);
        buttonPanel.add(editUserButton);
        buttonPanel.add(deleteUserButton);

        bottomPanel.add(buttonPanel, BorderLayout.SOUTH);

        panel.add(bottomPanel, BorderLayout.CENTER);
        return panel;

    }
    public JPanel createSystemManagementPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        JPanel topJPanel = new JPanel(new GridBagLayout());
        JPanel nextJPanel = new JPanel(new GridBagLayout());
        nextJPanel.setSize(topJPanel.getSize());

        // 设置topJPanel的边框
        topJPanel.setBorder(BorderFactory.createTitledBorder("管理员系统设置"));
        nextJPanel.setBorder(BorderFactory.createTitledBorder("图书种类设置"));


        GridBagConstraints gbc = new GridBagConstraints();

        // 设置revise按钮
        JButton revise = new JButton("修改管理员密码");
        revise.setOpaque(true);
        revise.setForeground(Color.white);
        revise.setBackground(new Color(135, 206, 250));
        revise.setBorderPainted(false);
        revise.setFont(revise.getFont().deriveFont(15f));
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5); // 添加间隔
        gbc.gridx = 0;
        gbc.gridy = 0;
        topJPanel.add(revise, gbc);
        revise.addActionListener(e -> {
            ResivePasswordDialog();
        });


        JButton Addbook = new JButton("添加图书种类");
        Addbook.setOpaque(true);
        Addbook.setForeground(Color.white);
        Addbook.setBackground(new Color(135, 206, 250));
        Addbook.setBorderPainted(false);
        Addbook.setFont(Addbook.getFont().deriveFont(15f));
        gbc.gridx = 0;
        gbc.gridy = 0;
        nextJPanel.add(Addbook, gbc);
        Addbook.addActionListener(e -> {
            createAddTypeDialog();
        });



        // 设置Deletebook按钮
        JButton Deletebook = new JButton("删除图书种类");
        Deletebook.setOpaque(true);
        Deletebook.setForeground(Color.white);
        Deletebook.setBackground(new Color(135, 206, 250));
        Deletebook.setBorderPainted(false);
        Deletebook.setFont(Deletebook.getFont().deriveFont(15f));
        gbc.gridx = 1;
        nextJPanel.add(Deletebook, gbc);
        Deletebook.addActionListener(e -> {
            ResivePasswordDialog();
        });

        // 将topJPanel和nextJPanel添加到panel中
        gbc.insets = new Insets(10, 10, 10, 10); // 添加适当的空隙
        panel.add(topJPanel, BorderLayout.BEFORE_FIRST_LINE);
        panel.add(nextJPanel, BorderLayout.AFTER_LAST_LINE);

        return panel;
    }
    public int getbook_number(){
        int booknumber = 0;
        try {
            // 获取数据库连接
            Connection connection = JDBC.getConnection();
            System.out.println("数据库连接成功");
            // 在这里执行数据库操作
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM booklist";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                if (id != 0){
                    booknumber++;
                }
            }
            resultSet.close();
            statement.close();
        } catch (ClassNotFoundException ex) {
            System.out.println("找不到 MySQL JDBC 驱动程序");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            ex.printStackTrace();
        }
        return booknumber;
    }
    public int getborrow_number(){
        int booknumber = 0;
        try {
            // 获取数据库连接
            Connection connection = JDBC.getConnection();
            System.out.println("数据库连接成功");
            // 在这里执行数据库操作
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM borrowlist";
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                if (id != 0){
                    booknumber++;
                }
            }
            resultSet.close();
            statement.close();
        } catch (ClassNotFoundException ex) {
            System.out.println("找不到 MySQL JDBC 驱动程序");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            ex.printStackTrace();
        }
        return booknumber;
    }
    private String getCurrentTime() {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置时区为北京时间
        return dateFormat.format(new Date());
    }
    //显示弹窗
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(400,300, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
    public static void createCenteredDialog() {
        JDialog dialog = new JDialog( );
        dialog.setLayout(new BorderLayout());

        // 创建输入框
        JTextField inputField = new JTextField(20);
        inputField.setPreferredSize(new Dimension(30, 30));

// 添加提示文本
        inputField.setToolTipText("请输入您要归还的图书的id");

// 创建标签显示 "id："
        JLabel idLabel = new JLabel("id：");

        dialog.add(idLabel,BorderLayout.WEST);
        // 创建按钮
        JButton actionButton = new JButton("确定");
        actionButton.setPreferredSize(new Dimension(30, 30));
        actionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(DeleteBook(Integer.parseInt(inputField.getText())))
                {
                    new NoticeJFrame("删除成功");
                }

            }
        });
        // 创建面板容纳输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(inputField, BorderLayout.NORTH);
        panel.add(actionButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void AddBookDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());


        // 创建标签和输入框
        String[] labels = {"ID:", "Group:", "Title:", "Author:", "Publisher:", "Price:", "Quantity:", "ISBN:"};
        JTextField[] inputFields = new JTextField[labels.length];

        JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
        JPanel inputPanel = new JPanel(new GridLayout(labels.length, 1));

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel(labels[i]);
            JTextField inputField = new JTextField(20);
            inputFields[i] = inputField;

            labelPanel.add(label);
            inputPanel.add(inputField);
        }

        // 创建按钮
        JButton addButton = new JButton("添加");
        addButton.addActionListener(e -> {

            int id =Integer.parseInt(inputFields[0].getText());
            String groups = inputFields[1].getText();
            String name = inputFields[2].getText();
            String author = inputFields[3].getText();
            String press = inputFields[4].getText();
            double price =Double.parseDouble(inputFields[5].getText());
            int quantity = Integer.parseInt(inputFields[6].getText());
            String isbn = inputFields[7].getText();
            if(Objects.equals(SelectISBNbook(isbn).getIsbn(), isbn))
            {
                new NoticeJFrame("当前ISBN已经存在！");
            }else {
                if(addBook(id,groups,name,author,press,price,quantity,isbn))
                {
                    new NoticeJFrame("添加成功");
                }
            }


        });

        // 创建面板容纳标签、输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(inputPanel, BorderLayout.CENTER);
        panel.add(addButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void UpdateBookDialog() {
        JDialog dialog = new JDialog();
        dialog.setTitle("图书信息修改《请先使用ISBN码查询图书！！》");
        dialog.setLayout(new BorderLayout());

        // 创建标签和输入框
        String[] labels = {"ID:", "Group:", "Title:", "Author:", "Publisher:", "Price:", "Quantity:", "ISBN:"};
        JTextField[] inputFields = new JTextField[labels.length];

        JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
        JPanel inputPanel = new JPanel(new GridLayout(labels.length, 1));

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel(labels[i]);
            JTextField inputField = new JTextField(20);
            inputFields[i] = inputField;

            labelPanel.add(label);
            inputPanel.add(inputField);
        }

        // 创建按钮
        JButton queryButton = new JButton("查询");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Book book=SelectISBNbook(inputFields[7].getText());
                if(book!=null)
                {
                    inputFields[0].setText(String.valueOf(book.getId()));
                    inputFields[1].setText(book.getGroups());
                    inputFields[2].setText(book.getName());
                    inputFields[3].setText(book.getAuthor());
                    inputFields[3].setText(String.valueOf(book.getPrice()));
                    inputFields[5].setText(book.getPress());
                    inputFields[6].setText(String.valueOf(book.getQuantity()));
                }
            }
        });

        JButton updateButton = new JButton("修改");


        updateButton.addActionListener(e -> {
            int id =Integer.parseInt(inputFields[0].getText());
            String groups = inputFields[1].getText();
            String name = inputFields[2].getText();
            String author = inputFields[3].getText();
            String press = inputFields[4].getText();
            double price =Double.parseDouble(inputFields[5].getText());
            int quantity = Integer.parseInt(inputFields[6].getText());
            String isbn = inputFields[7].getText();
            DeleteBook(Integer.parseInt(inputFields[0].getText()));
            addBook(id,groups,name,author,press,price,quantity,isbn);
        });
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(inputPanel, BorderLayout.CENTER);
        panel.add(queryButton, BorderLayout.SOUTH);
        panel.add(updateButton, BorderLayout.EAST);

        dialog.add(panel, BorderLayout.CENTER);

        dialog.pack();
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void ResivePasswordDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new GridBagLayout());

        // 创建输入框
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);

        JLabel last_password_JLabel = getJLabel("旧密码:", Color.BLACK, Color.WHITE, "黑体", 12, 30, 10);
        panel.add(last_password_JLabel, gbc);

        gbc.gridx = 1;
        JTextField last_password = new JTextField(20);
        last_password.addFocusListener(getFocusListener(last_password,"输入原密码"));
        last_password.setPreferredSize(new Dimension(150, 30));
        panel.add(last_password, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel new_password_JLabel = getJLabel("新密码:", Color.BLACK, Color.WHITE, "黑体", 12, 30, 10);
        panel.add(new_password_JLabel, gbc);

        gbc.gridx = 1;
        JTextField new_password = new JTextField(20);
        new_password.addFocusListener(getFocusListener(new_password,"输入新密码"));
        new_password.setPreferredSize(new Dimension(150, 30));
        panel.add(new_password, gbc);

        // 创建按钮
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        JButton actionButton = new JButton("确定");
        actionButton.setPreferredSize(new Dimension(100, 30));
        actionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (user != null && user.getPassword().equals(last_password.getText())) {
                    if (user != null && user.getPassword().equals(last_password.getText())) {
                        updateUserPassword(user.getUsername(), new_password.getText());
                        new NoticeJFrame("修改成功！");
                    } else {
                        new NoticeJFrame("旧密码不正确！请重新输入！");
                    }

                } else {
                    new NoticeJFrame("旧密码不正确！请重新输入！");
                }
            }
        });
        panel.add(actionButton, gbc);

        // 将面板添加到对话框的中心
        dialog.add(panel, new GridBagConstraints());

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void createAddUserDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());

        // 创建标签和输入框
        String[] labels = {"ID:", "Group:", "name:","Username:", "password:", "gender:", "id_card:", "phone:", "identity:","book_count:","state:"};
        JTextField[] inputFields = new JTextField[labels.length];

        JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
        JPanel inputPanel = new JPanel(new GridLayout(labels.length, 1));

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel(labels[i]);
            JTextField inputField = new JTextField(20);
            inputFields[i] = inputField;

            labelPanel.add(label);
            inputPanel.add(inputField);
        }

        // 创建按钮
        JButton addButton = new JButton("添加");
        addButton.addActionListener(e -> {
            int id =Integer.parseInt(inputFields[0].getText());
            String groups = inputFields[1].getText();
            String name=inputFields[2].getText();
            String username = inputFields[3].getText();
            String password = inputFields[4].getText();
            String gender = inputFields[5].getText();
            int id_card =Integer.parseInt(inputFields[6].getText());
            String phone = inputFields[7].getText();
            String identity = inputFields[8].getText();
            int book_count =Integer.parseInt(inputFields[9].getText());
            int state =Integer.parseInt(inputFields[10].getText());
            SelectUser();
            for (int i = 0; i < UserList.size(); i++) {
                User user=UserList.get(i);
                if(user.getId_card()!=id_card && user.getId()!=id)
                {
                    addUser(id,groups,name,username,password,gender,id_card,phone,identity,book_count,state);
                    new NoticeJFrame("添加成功");
                }else {
                    System.out.println("该id已存在");
                    new NoticeJFrame("该id已存在");
                }
            }

        });

        // 创建面板容纳标签、输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(inputPanel, BorderLayout.CENTER);
        panel.add(addButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void DeleteUserDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());

        JLabel reminderJLabel = getJLabel("请输入您要删除的用户的ID", Color.BLACK, Color.CYAN, "仿宋", 16, 30, 10);
        dialog.add(reminderJLabel, BorderLayout.NORTH);

        // 创建面板容纳输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        JTextField inputField = new JTextField(20);
        inputField.setPreferredSize(new Dimension(30, 30));
        JButton actionButton = new JButton("确定");
        actionButton.setPreferredSize(new Dimension(30, 30));
        actionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (DeleteUser(Integer.parseInt(inputField.getText()))) {
                    actionButton.setText("删除成功");
                }
            }
        });
        panel.add(inputField, BorderLayout.NORTH);
        panel.add(actionButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.setSize(300, 150); // 设置合适的大小
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public void showSelectedPanel(int selectedIndex,JPanel contentPanel) {
        contentPanel.removeAll();
        JPanel selectedPanel = null;

        switch (selectedIndex) {
            case 0:
                OverBorrowList.clear();
                AllborrowList.clear();
                BookList.clear();
                OperationList.clear();
                SelectAllBorrow();
                SelectALLBook();
                Selectoperation();
                selectedPanel = createHomePanel();
                break;
            case 1:
                BookList.clear();
                SelectALLBook();
                selectedPanel = createBookManagementPanel();
                break;
            case 2:
                selectedPanel = createBorrowBookPanel();
                break;
            case 3:
                selectedPanel = createReturnBookPanel();
                break;
            case 4:
                SelectOverBorrow();
                OverBorrowList.clear();
                AllborrowList.clear();
                SelectAllBorrow();
                selectedPanel = createOverdueQueryPanel();
                break;
            case 5:
                UserList.clear();
                SelectUser();
                selectedPanel = createUserManagementPanel();
                break;
            case 6:
                selectedPanel = createSystemManagementPanel();
                break;
            default:
                break;
        }
        contentPanel.add(selectedPanel, BorderLayout.CENTER);
        contentPanel.revalidate();
        contentPanel.repaint();

        JLabel label = new JLabel("当前子夹: " + Library_ManagementJFrame.getTabbedPane().getTitleAt(selectedIndex));
        label.setFont(label.getFont().deriveFont(20f));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        contentPanel.add(label, BorderLayout.NORTH);
    }

    //查询用户
    public static void SelectUser() {
        String query = "SELECT * FROM user";

        try (Connection conn = JDBC.getConnection()) {
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {
                while (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id")); // 假设email是数据库中的字段名
                    user.setName(rs.getString("name")); // 假设name是数据库中的字段名
                    user.setGroups(rs.getString("groups"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setGender(rs.getString("gender"));
                    user.setId_card(rs.getInt("id_card"));
                    user.setPhone(rs.getString("phone"));

                    user.setIdentity(rs.getString("identity"));
                    user.setBook_count(rs.getInt("book_count"));
                    user.setState(rs.getInt("state"));
                    // 设置其他属性
                    UserList.add(user);
                }

                // 在这里使用userList的数据，例如传递给其他方法或存储在全局变量中

                if (!UserList.isEmpty()) {
                    System.out.println("查询成功成功");
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception s) {
                        s.printStackTrace();
                    }
                } else {
                    System.out.println("查询失败");
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static void SelectOverBorrow(){
        SelectAllBorrow();
        int count = 1;
        for(Borrow borrow:AllborrowList){
            if (borrow.Timeout_or_not()){
                OverBorrowList.add(borrow);
                System.out.println("找到"+count+"本超时未归还的书");
                count++;
            }
        }
    }
    public static void UpdateUserDialog() {
        JDialog dialog = new JDialog();
        dialog.setTitle("用户信息修改《请先使用Id查询用户！！》");
        dialog.setLayout(new BorderLayout());

        // 创建标签和输入框
        String[] labels = {"ID:", "Group:", "name","Username:", "paddword:", "gender:", "id_card:", "phone:", "identity:","book_count:","state:"};
        JTextField[] inputFields = new JTextField[labels.length];

        JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
        JPanel inputPanel = new JPanel(new GridLayout(labels.length, 1));

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel(labels[i]);
            JTextField inputField = new JTextField(20);
            inputFields[i] = inputField;
            labelPanel.add(label);
            inputPanel.add(inputField);
        }

        // 创建按钮
        JButton queryButton = new JButton("查询");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id =Integer.parseInt(inputFields[0].getText());
                SelectUser();
                for (int i = 0; i < UserList.size(); i++) {
                    User user=UserList.get(i);
                    if(id== user.getId())
                    {
                        inputFields[0].setText(String.valueOf(user.getId()));
                        inputFields[1].setText(user.getGroups());
                        inputFields[2].setText(user.getName());
                        inputFields[3].setText(user.getUsername());
                        inputFields[4].setText(user.getPassword());
                        inputFields[5].setText(user.getGender());
                        inputFields[6].setText(String.valueOf(user.getId_card()));
                        inputFields[7].setText(user.getPhone());
                        inputFields[8].setText(user.getIdentity());
                        inputFields[9].setText(String.valueOf(user.getBook_count()));
                        inputFields[10].setText(String.valueOf(user.getState()));
                    }
                }

            }
        });

        JButton updateButton = new JButton("修改");

        updateButton.addActionListener(e -> {
            int id =Integer.parseInt(inputFields[0].getText());
            String groups = inputFields[1].getText();
            String name= inputFields[2].getText();
            String username = inputFields[3].getText();
            String password = inputFields[4].getText();
            String gender = inputFields[5].getText();
            int id_card =Integer.parseInt(inputFields[6].getText());
            String phone = inputFields[7].getText();
            String identity = inputFields[8].getText();
            int book_count =Integer.parseInt(inputFields[9].getText());
            int state =Integer.parseInt(inputFields[10].getText());
            DeleteUser(id);
            addUser(id,groups,name,username,password,gender,id_card,phone,identity,book_count,state);
        });
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(inputPanel, BorderLayout.CENTER);
        panel.add(queryButton, BorderLayout.SOUTH);
        panel.add(updateButton, BorderLayout.EAST);

        dialog.add(panel, BorderLayout.CENTER);

        dialog.pack();
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }
    public static void DeleteBorrowDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());

        // 创建标签显示 "id："
        JLabel idLabel = new JLabel("id：");

        // 创建输入框
        JTextField inputField = new JTextField(20);
        inputField.setPreferredSize(new Dimension(30, 30));
        inputField.setToolTipText("请输入您要归还的图书的id");

        // 创建按钮
        JButton actionButton = new JButton("确定");
        actionButton.setPreferredSize(new Dimension(30, 30));
        actionButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SelectAllBorrow();
                for (int i = 0; i < AllborrowList.size(); i++) {
                    Borrow borrow = AllborrowList.get(i);
                    if (Integer.parseInt(inputField.getText()) == borrow.getId()) {
                        if (DeleteBorrow(Integer.parseInt(inputField.getText()))) {
                            Selectoperation();
                            AddOperation(OperationList.size() + 1, user.getUsername(), borrow.getBook_name(), "归还图书");
                            new NoticeJFrame("归还成功");
                        }
                        else{
                            new NoticeJFrame("没有找到该借书记录");
                        }
                    }
                }
            }
        });

        // 创建面板容纳标签、输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(idLabel, BorderLayout.WEST);
        panel.add(inputField, BorderLayout.CENTER);
        panel.add(actionButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }

    //查询所有图书
    public void SelectALLBook() {
        String query = "SELECT * FROM booklist";

        try (Connection conn = JDBC.getConnection()) {
            try (Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query)) {
                ResultSetMetaData metaData = rs.getMetaData();
                int book_count = 0;


                while (rs.next()) {
                    Book books = new Book();
                    books.setId(rs.getInt("id")); // 假设email是数据库中的字段名
                    books.setName(rs.getString("name")); // 假设name是数据库中的字段名
                    books.setGroups(rs.getString("groups"));
                    books.setAuthor(rs.getString("author"));
                    books.setPress(rs.getString("press"));
                    books.setPrice(rs.getDouble("price"));
                    books.setQuantity(rs.getInt("quantity"));
                    books.setIsbn(rs.getString("isbn"));
                    // 设置其他属性
                    BookList.add(books);
                    book_count++;
                    System.out.println("有"+book_count+"本书加入到booklist");
                }

                // 在这里使用userList的数据，例如传递给其他方法或存储在全局变量中

                if (!BookList.isEmpty()) {
                    System.out.println("查询boollist成功");

                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception s) {
                        s.printStackTrace();
                    }
                } else {
                    System.out.println("查询失败");
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Book SelectISBNbook(String isbn) {
        String query = "SELECT * FROM booklist where isbn='"+isbn+"'";
        Book book = new Book();
        try (Connection conn = JDBC.getConnection()) {
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {
                ResultSetMetaData metaData = rs.getMetaData();
                while (rs.next()) {
                    book.setId(rs.getInt("id")); // 假设email是数据库中的字段名
                    book.setName(rs.getString("name")); // 假设name是数据库中的字段名
                    book.setGroups(rs.getString("groups"));
                    book.setAuthor(rs.getString("author"));
                    book.setPress(rs.getString("press"));
                    book.setPrice(rs.getDouble("price"));
                    book.setQuantity(rs.getInt("quantity"));
                    book.setIsbn(rs.getString("isbn"));
                }

                // 在这里使用userList的数据，例如传递给其他方法或存储在全局变量中

                if (book != null) {
                    System.out.println("查询ISBN成功");
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception s) {
                        s.printStackTrace();
                    }
                } else {
                    System.out.println("查询ISBN失败");
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return book;
    }
    //更新图书数量
    public boolean updateBookquantity(String isbn) {
        Book book1=SelectISBNbook(isbn);
        int quantity=(book1.getQuantity())-1;
        System.out.println(quantity);
        if(quantity>0)
        {
            String query = "UPDATE booklist SET quantity = '"+quantity+"' WHERE isbn = '"+isbn+"'";
            try (Connection conn = JDBC.getConnection()) {
                try (PreparedStatement stmt = conn.prepareStatement(query)) {

                    int rowsAffected = stmt.executeUpdate();
                    if (rowsAffected > 0) {
                        System.out.println("更新书籍成功");
                        return true;
                    } else {
                        System.out.println("更新书籍失败");
                        return false;
                    }
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                return false;
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }else{
            return  false;
        }
    }
    //查询所有借阅记录
    public static void SelectAllBorrow() {

        String query = "SELECT * FROM borrowlist";
        try (Connection conn = JDBC.getConnection()) {
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {
                ResultSetMetaData metaData = rs.getMetaData();
                while (rs.next()) {
                    Borrow borrow = new Borrow();
                    borrow.setId(rs.getInt("id")); // 假设email是数据库中的字段名
                    borrow.setBook_name(rs.getString("book_name")); // 假设name是数据库中的字段名
                    borrow.setIsbn(rs.getString("isbn"));
                    borrow.setUsername(rs.getString("username"));
                    borrow.setId_card(rs.getString("id_card"));
                    borrow.setPhone(rs.getString("phone"));
                    borrow.setBorrow_time(rs.getTimestamp("borrow_time"));
                    borrow.setReturn_time(rs.getTimestamp("return_time"));
                    // 设置其他属性
                    long currentTimeMillis = System.currentTimeMillis();
                    Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
                    Date date1 = new Date(currentTimestamp.getTime());
                    Date date2 = new Date(borrow.getReturn_time().getTime());
                    int comparison = date1.compareTo(date2);
                    if(comparison>0)
                    {
                        OverBorrowList.add(borrow);
                    }
                    AllborrowList.add(borrow);
                }

                // 在这里使用userList的数据，例如传递给其他方法或存储在全局变量中

                if (!OverBorrowList.isEmpty()) {
                    System.out.println("查询成功");
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception s) {
                        s.printStackTrace();
                    }
                } else {
                    System.out.println("查询失败");
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //查询操作信息
    public static void Selectoperation() {
        String query = "SELECT * FROM operationlist";
        int count = 1;

        try (Connection conn = JDBC.getConnection()) {
            try (Statement stmt = conn.createStatement();
                 ResultSet rs = stmt.executeQuery(query)) {
                ResultSetMetaData metaData = rs.getMetaData();
                while (rs.next()) {
                    Operation operation = new Operation();
                    operation.setId(rs.getInt("Id")); // 假设email是数据库中的字段名
                    operation.setBook_name(rs.getString("book_name")); // 假设name是数据库中的字段名
                    operation.setName(rs.getString("name"));
                    operation.setInfo(rs.getString("info"));
                    operation.setTime(rs.getTimestamp("time"));
                    OperationList.add(operation);
                    System.out.println("有"+count+"个操作加入到OperationList");
                }

                // 在这里使用userList的数据，例如传递给其他方法或存储在全局变量中

                if (!OperationList.isEmpty()) {
                    System.out.println("查询成功");
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception s) {
                        s.printStackTrace();
                    }
                } else {
                    System.out.println("查询失败");
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //添加操作信息
    public static boolean AddOperation(int id, String name, String book_name, String info) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        try {
// 连接数据库
            Connection connection = JDBC.getConnection();

            // 创建 PreparedStatement 对象
            String sql = "INSERT INTO operationlist (id, time, name,book_name,info) VALUES (?, ?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);

            // 设置参数值
            statement.setInt(1, id);
            statement.setTimestamp(2, currentTimestamp);
            statement.setString(3, name);
            statement.setString(4, book_name);
            statement.setString(5, info);

            // 执行插入操作
            int rowsAffected = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            connection.close();

            System.out.println(rowsAffected + " 行数据插入数据库operationlist成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
    //添加结束信息
    public boolean AddBorrowBook(int id,String book_name,String isbn,String username,String id_card,String phone) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTimestamp);

        // 将 calendar 的时间增加 24 小时
        calendar.add(Calendar.HOUR_OF_DAY, 24);

        // 获取增加后的时间戳
        Timestamp returnpTimestamp = new Timestamp(calendar.getTimeInMillis());
        try {
            // 连接数据库
            Connection connection = JDBC.getConnection();

            // 创建 PreparedStatement 对象
            String sql = "INSERT INTO borrowlist (id,book_name,isbn,username,id_card,phone,borrow_time,return_time) VALUES (?,?,?,?,?,?,?,?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            System.out.println(isbn);
            // 设置参数值
            statement.setInt(1, id);
            statement.setString(2, book_name);
            statement.setString(3,isbn);
            statement.setString(4, username);
            statement.setString(5, id_card);
            statement.setString(6, phone);
            statement.setTimestamp(7, currentTimestamp);
            statement.setTimestamp(8, returnpTimestamp);

            // 执行插入操作
            int rowsAffected = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            connection.close();

            System.out.println(rowsAffected + " 行数据插入成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
    //删除图书
    public static boolean DeleteBook(int id) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        String deleteQuery = "DELETE FROM booklist WHERE id='"+id+"'";

        try {
            // 连接到数据库
            Connection connection = JDBC.getConnection();
            // 创建 Statement 对象
            Statement statement = connection.createStatement();

            // 执行删除操作
            int rowsAffected = statement.executeUpdate(deleteQuery);
            if(rowsAffected>=0)
            {
                System.out.println("删除了 " + rowsAffected + " 行数据。");
                return true;
            }else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    //添加图书
    public static boolean addBook(int id, String groups, String name, String author, String press, double price, int quantity, String isbn) {
        try {
            // 连接数据库
            Connection connection = JDBC.getConnection();

            String sql = "INSERT INTO booklist (`id`, `groups`, `name`, `author`, `press`, `price`, `quantity`, `isbn`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";


            PreparedStatement statement = connection.prepareStatement(sql);

            // 设置参数值
            statement.setInt(1, id);
            statement.setString(2, groups);
            statement.setString(3, name);
            statement.setString(4, author);
            statement.setString(5, press);
            statement.setDouble(6, price);
            statement.setInt(7, quantity);
            statement.setString(8, isbn);

            // 执行插入操作
            int rowsAffected = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            connection.close();
            if(rowsAffected>0)
            {
                System.out.println("图书添加成功");
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    //删除用户
    public static boolean DeleteUser(int id) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        String deleteQuery = "DELETE FROM user WHERE id='"+id+"'";

        try {
            // 连接到数据库
            Connection connection = JDBC.getConnection();

            // 创建 Statement 对象
            Statement statement = connection.createStatement();

            // 执行删除操作
            int rowsAffected = statement.executeUpdate(deleteQuery);
            if(rowsAffected>=0)
            {
                System.out.println("删除了 " + rowsAffected + " 行数据。");
                return true;
            }else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    //更新用户密码
    public static boolean updateUserPassword(String username, String password) {
        String query = "UPDATE user SET password = ? WHERE username = ?";

        try (Connection conn = JDBC.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query)) {

            stmt.setString(1, password);
            stmt.setString(2, username);

            int rowsAffected = stmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("更新用户密码成功");
                return true;
            } else {
                System.out.println("更新用户密码失败");
                return false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //删除borrow
    public static boolean DeleteBorrow(int id) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
        String deleteQuery = "DELETE FROM borrowlist WHERE id='"+id+"'";

        try {
            // 连接到数据库
            Connection connection = JDBC.getConnection();

            // 创建 Statement 对象
            Statement statement = connection.createStatement();

            // 执行删除操作
            int rowsAffected = statement.executeUpdate(deleteQuery);
            if(rowsAffected>=0)
            {
                System.out.println("删除了 " + rowsAffected + " 行数据。");
                return true;
            }else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    //添加user
    public static boolean addUser(int id, String groups, String name,String username, String password, String gender, int id_card, String phone, String identity,int book_count,int state) {
        try {
            // 连接数据库
            Connection connection = JDBC.getConnection();

            // 创建 PreparedStatement 对象
            String sql = "INSERT INTO user  (`id`, `groups`, `name`,`username`, `password`, `gender`, `id_card`, `phone`, `identity`,`book_count`,`state`) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?,?,?)";


            PreparedStatement statement = connection.prepareStatement(sql);

            // 设置参数值
            statement.setInt(1, id);
            statement.setString(2, groups);
            statement.setString(3, name);
            statement.setString(4, username);
            statement.setString(5, password);
            statement.setString(6, gender);
            statement.setInt(7, id_card);
            statement.setString(8, phone);
            statement.setString(9, identity);
            statement.setInt(10, book_count);
            statement.setInt(11, state);
            System.out.println(sql);
            // 执行插入操作
            int rowsAffected = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            connection.close();
            if(rowsAffected>0)
            {
                System.out.println("用户添加成功");
                return true;
            }else {

                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    public static FocusListener getFocusListener(JTextField text, String a){
        FocusListener FocusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (text.getText().equals(a)){
                    text.setText("");     //将提示文字清空
                    text.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                if (text.getText().equals("")){
                    text.setForeground(Color.gray); //将提示文字设置为灰色
                    text.setText(a);     //显示提示文字
                }
            }
        };
        return FocusListener;
    }
    public static void createAddTypeDialog() {
        JDialog dialog = new JDialog();
        dialog.setLayout(new BorderLayout());


        // 创建标签和输入框
        String[] labels = {"ID:", "Type_name:", "Type_id:"};
        JTextField[] inputFields = new JTextField[labels.length];

        JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
        JPanel inputPanel = new JPanel(new GridLayout(labels.length, 1));

        for (int i = 0; i < labels.length; i++) {
            JLabel label = new JLabel(labels[i]);
            JTextField inputField = new JTextField(20);
            inputFields[i] = inputField;

            labelPanel.add(label);
            inputPanel.add(inputField);
        }

        // 创建按钮
        JButton addButton = new JButton("添加");
        addButton.addActionListener(e -> {
            int id =Integer.parseInt(inputFields[0].getText());
            String type_name = inputFields[1].getText();
            String type_id=inputFields[2].getText();

            for (int i = 0; i < TypeList.size(); i++) {
                Type type=TypeList.get(i);
                if(type.getType_name()!=type_name && type.getType_id()!=type_id)
                {
                    add_type(id,type_name,type_id);
                    new NoticeJFrame("添加成功");
                }else {
                    System.out.println("该id已存在");
                    new NoticeJFrame("该id已存在");
                }
            }

        });

        // 创建面板容纳标签、输入框和按钮
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelPanel, BorderLayout.WEST);
        panel.add(inputPanel, BorderLayout.CENTER);
        panel.add(addButton, BorderLayout.SOUTH);

        // 将面板添加到对话框的中心
        dialog.add(panel, BorderLayout.CENTER);

        // 设置对话框大小并居中显示
        dialog.pack();
        dialog.setLocationRelativeTo(null); // 居中显示
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
    }

    public static boolean add_type(int id,String type_name,String type_id){
        try {
            // 连接数据库
            Connection connection = JDBC.getConnection();

            // 创建 PreparedStatement 对象
            String sql = "INSERT INTO typelist  (`id`, `type_name`, `type_id`) VALUES (?, ?, ?)";


            PreparedStatement statement = connection.prepareStatement(sql);

            // 设置参数值
            statement.setInt(1, id);
            statement.setString(2, type_name);
            statement.setString(3, type_id);

            System.out.println(sql);
            // 执行插入操作
            int rowsAffected = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            connection.close();
            if(rowsAffected>0)
            {
                System.out.println("图书种类添加成功");
                return true;
            }else {

                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}